package com.kgc.cgprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgapi.pojo.CGDDPojo;
import com.kgc.cgapi.pojo.CGTHInfo;
import com.kgc.cgapi.pojo.CGTHPojo;
import com.kgc.cgprovider.dao.CGTHDao;
import com.kgc.cgprovider.service.CGServ;
import com.kgc.cgprovider.service.CGTHService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CGTHServiceImpl implements CGTHService {
    private Logger logger = LoggerFactory.getLogger(CGTHServiceImpl.class);
    @Resource
    private CGTHDao cgthDao;

    @Resource
    private CGServ cgServ;

    @Override
    public ResponseMsg getCGTHPage(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<CGTHPojo> list = cgthDao.getCGTHPage(map);
        PageInfo<CGTHPojo> pageInfo = new PageInfo<>(list);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg addCGTH(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        String code ="CGTH"+"-"+ cgServ.getCode("cg_fcgth");
        map.put("code",code);
        Date date = new Date();
        map.put("create_date",date);

        int result = cgthDao.addCGTHPojo(map);
        logger.info("addCGTHPojo  + map {} result:{}",map,result);
        if (result <= 0){
            return responseMsg;
        }

        for (Map<String,Object> info : arr){
            info.put("major_code",code);
            info.put("create_date",date);
            info.put("create_id",map.get("create_id"));
            result = cgthDao.addCGTHInfo(info);
            logger.info("addCGTHInfo  + info {} result:{}",info,result);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setData(code);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getCGTH(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);

        List<CGTHPojo> pojoList = cgthDao.getCGTHPage(map);
        for(CGTHPojo pojo:pojoList){
            List<CGTHInfo> list = cgthDao.getCGTHInfo(pojo.getCode());
            pojo.setList(list);
        }
        responseMsg.setData(pojoList);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiCGTH(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);

        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        Date date = new Date();
        map.put("modi_date",date);
        String code = map.get("code").toString();
        Object uid = map.get("uid");
        int result = cgthDao.modiCGTH(map);
        logger.info("modiCGTH  + map {} result:{}",map,result);
        if (result <= 0){
            return responseMsg;
        }
        result = cgthDao.delCGTH(code);
        logger.info("delCGTH  + map {} result:{}",map,result);

        for (Map<String,Object> info : arr){
            if(   info.get("major_code") == null ||  info.get("major_code").toString().equals("")){
                info.put("major_code",code);
                info.put("create_date",date);
                info.put("create_id",map.get("modi_id"));
            }else {
                info.put("modi_id",map.get("modi_id"));
                info.put("modi_date",date);
            }
            result = cgthDao.addCGTHInfo(info);
            logger.info("addCGTHInfo  + info {} result:{}",info,result);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        return responseMsg;

    }

    @Override
    public ResponseMsg updateCGTH(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = cgthDao.updateCGTH(map);
        logger.info("updateCGTH  + map {} result:{}",map,result);
        if (result <= 0){
            return responseMsg;
        }
        List<CGTHPojo> pojoList = cgthDao.getCGTHPage(map);
        CGTHPojo pojo = pojoList.get(0);
        if (pojo.getTotal() == pojo.getFinish_amount()){
            Map<String, Object> params = new HashMap<>();
            params.put("code",pojo.getCode());
            params.put("status",11);
            result = cgthDao.updateCGTH(params);
        }
        responseMsg.setCode(200);
        return responseMsg;

    }


}
